import tkinter as tk
from PIL import ImageTk
from search import search, finish


class MainWindow():
    def __init__(self):

        self.__win = tk.Tk(className='百度会员')
        # 设置窗口长宽是否可变，0为不可变
        # self.__win.resizable(1, 1)
        # 窗口大小
        self.__win.geometry('800x500')
        # 窗口图标
        # self.__win.iconbitmap('./img/bilibili.ico')

        tk.Label(text='搜索范围（大于八）：').place(x=80, y=10)

        self.__start = tk.Entry(self.__win)
        self.__start.place(x=240, y=12, width=60)
        tk.Label(text='：', font=('楷体', 20)).place(x=340, y=2.5)
        self.__end = tk.Entry(self.__win)
        self.__end.place(x=420, y=12, width=60)
        # delete方法第一个参数表示从前或后开始删除，第二个参数表示一次删除的字符数
        tk.Button(self.__win, text='清空', command=self.clear).place(x=540,
                                                                   y=10,
                                                                   width=60,
                                                                   height=25)
        tk.Button(self.__win, text='确定', command=self.search).place(x=600,
                                                                    y=10,
                                                                    width=60,
                                                                    height=25)
        # 进度条
        tk.Label(self.__win, text='搜索进度：').place(x=80, y=48)
        self.__canvas = tk.Canvas(self.__win, bg='white')
        self.__canvas.place(x=160, y=45, width=490, height=30)
        # 显示二维码的label
        self.__qrcode = tk.Label(self.__win)
        self.__qrcode.place(x=200, y=120)

        self.__win.mainloop()

    def search(self):

        # 如果是第二遍下载，先将进度条还原成白色
        # 进度条填充颜色
        fill_line = self.__canvas.create_rectangle(0, 0, 0, 30, fill='white')
        self.__canvas.coords(fill_line, (0, 0, 490, 30))
        self.__win.update()
        self.__s, total = search(int(self.__start.get()),
                                 int(self.__end.get()))
        # 创建画布填充形状
        fill_line = self.__canvas.create_rectangle(0, 0, 0, 30, fill='red')
        n = 0
        while n < total:
            while finish:
                n += 1
                finish.pop(0)

            self.__canvas.coords(fill_line, (0, 0, (n / total * 490), 30))
            # 根据标签删除内容
            self.__canvas.delete('txt')
            self.__canvas.create_text(245,
                                      15,
                                      text=f'{int(n / total * 100)}%',
                                      font=('楷体', 20),
                                      tags='txt')
            self.__win.update()
        self.__nums = self.__s.get_nums()
        if self.__nums:
            # 要显示的二维码在列表中的下标
            self.__i = 0
            # 要显示的图片变量必须是全局变量或是属性(别问为什么，问就是不知到)
            self.img = ImageTk.PhotoImage(
                self.__s.set_qrcode(self.__nums[self.__i]))
            self.__qrcode.configure(image=self.img)
            # 下一张
            tk.Button(self.__win,
                      text='＞',
                      font=('宋体', 22),
                      command=self.right).place(x=680, y=250)
            # 上一张
            tk.Button(self.__win, text='＜', font=('宋体', 22),
                      command=self.left).place(x=100, y=250)
            # 保存所有二维码
            tk.Button(self.__win, text='保存所有').place(x=700, y=80, width=60, height=25)
        else:
            self.__qrcode.configure(text='此范围无活动', fg='red', font=('楷体', 25))

    def clear(self):
        """清空输入框"""
        self.__start.delete(0, 100)
        self.__end.delete(0, 100)

    def left(self):
        if self.__i > 0:
            self.__i -= 1
            self.img = ImageTk.PhotoImage(
                self.__s.set_qrcode(self.__nums[self.__i]))
            self.__qrcode.configure(image=self.img)
            self.__win.update()

    def right(self):
        if self.__i < len(self.__nums) - 1:
            self.__i += 1
            self.img = ImageTk.PhotoImage(
                self.__s.set_qrcode(self.__nums[self.__i]))
            self.__qrcode.configure(image=self.img)
            self.__win.update()


if __name__ == '__main__':
    MainWindow()
